package com.lingyuan.warehouse.controller;

import com.alibaba.fastjson.JSON;
import com.lingyuan.common.core.domain.R;
import com.lingyuan.common.core.domain.vo.wms.WmsPurchaseDetailVO;
import com.lingyuan.common.core.domain.warehouse.pojo.PO.WmsPurchaseDetail;
import com.lingyuan.common.core.web.controller.BaseController;
import com.lingyuan.warehouse.domain.WmsPurchase;
import com.lingyuan.warehouse.domain.WmsPurchaseDto;
import com.lingyuan.warehouse.service.WmsPurchaseDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author：寇可往，我亦可往。
 * @Package：com.lingyuan.warehouse.controller
 * @Project：lingyuan-shopping-mall
 * @name：WmsPurchaseDetailController
 * @Date：2024/9/19 21:15
 */
@RestController
@RequestMapping("/wms")
@Slf4j
public class WmsPurchaseDetailController extends BaseController {

    @Autowired
    private WmsPurchaseDetailService wmsPurchaseDetailService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 获取采购需求列表
     * @param wmsPurchaseDetailVO
     * @return
     */
    @PostMapping("/purchaseDetailList")
    public com.lingyuan.common.core.web.page.TableDataInfo purchaseDetailList(
                          @RequestBody WmsPurchaseDetailVO wmsPurchaseDetailVO){
        log.info ( "ware list :  方法：{}  路经：{}  入参：{}", request.getMethod (),
                request.getRequestURI (), JSON.toJSONString ( wmsPurchaseDetailVO ) );
        this.startPage ();
        List<WmsPurchase> wareSkuList = wmsPurchaseDetailService.getPuchaseList(wmsPurchaseDetailVO);
        log.info ( "ware list :  方法：{}  路经：{}  出参：{}", request.getMethod (),
                request.getRequestURI (), JSON.toJSONString ( wareSkuList ) );
        return getDataTable ( wareSkuList );

    }

    /***
     * 添加修改采购需求
     * @param wmsPurchaseDetail
     * @return
     */
    @PostMapping("/purchaseDetailAdd")
    public R<Object> purchaseDetailAdddd(@RequestBody WmsPurchaseDto wmsPurchaseDetail){
        log.info ( "ware add :  方法：{}  路经：{}  入参：{}", request.getMethod (),
                request.getRequestURI (), JSON.toJSONString ( wmsPurchaseDetail ) );
       if ( wmsPurchaseDetail.getPurchaseDetailList ()!=null){
           wmsPurchaseDetailService.add( wmsPurchaseDetail.getPurchaseDetailList () );
       }
       if ( wmsPurchaseDetail.getPurchase ()!=null){
           wmsPurchaseDetailService.purchaseAdd(wmsPurchaseDetail.getPurchase ());
       }
        log.info ( "ware add :  方法：{}  路经：{}  出参：{}", request.getMethod ()
         , request.getRequestURI (),JSON.toJSONString ( wmsPurchaseDetail ));
        return R.ok ();
    }

    /**
     * 修改采购需求
     * @param wmsPurchaseDetail
     * @return
     */
    @PostMapping("/purchaseDetailUpdate")
    public R<Object> purchaseDetailUpdate(@RequestBody WmsPurchase wmsPurchaseDetail){
        log.info ( "ware update :  方法：{}  路经：{}  入参：{}", request.getMethod (),
                request.getRequestURI (), JSON.toJSONString ( wmsPurchaseDetail ) );
        wmsPurchaseDetailService.purchaseAllUpdate(wmsPurchaseDetail);
        log.info ( "ware update :  方法：{}  路经：{}  出参：{}", request.getMethod (), request.getRequestURI ()
           , JSON.toJSONString ( wmsPurchaseDetail));
        return R.ok ();
    }

    /**
     * 删除采购需求
     * @param id
     * @return
     */
    @GetMapping("/purchaseDetailRemoveById")
    public R<Object> purchaseDetailRemoveById(@RequestParam("id") Long id){
        log.info ( "ware remove :  方法：{}  路经：{}  入参：{}", request.getMethod (),
                request.getRequestURI (), JSON.toJSONString ( id ) );
        wmsPurchaseDetailService.delete(id);
        log.info ( "ware remove :  方法：{}  路经：{}  出参：{}", request.getMethod (), request.getRequestURI ()
         , JSON.toJSONString ( id));
        return R.ok ();
    }


    /**
     * 审批接口合并
     * @param wmsPurchaseDetailVO
     * @return
     */
    @PostMapping("/mergeCombine")
    public R mergeCombine(@RequestBody List<WmsPurchaseDetailVO> wmsPurchaseDetailVO){
        log.info ( "ware list :  方法：{}  路经：{}  入参：{}", request.getMethod (),
                request.getRequestURI (), JSON.toJSONString ( wmsPurchaseDetailVO ) );
          wmsPurchaseDetailService.mergePurchase(wmsPurchaseDetailVO);
        log.info ( "ware list :  方法：{}  路经：{}  出参：{}", request.getMethod (),
                request.getRequestURI (), JSON.toJSONString ( null ) );
        return R.ok ();

    }
}
